include(FindPython)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(GENERATE_SCRIPT ${CLR_DIR}/scripts/genEventPipe.py)
set(GENERATE_COMMAND ${Python_EXECUTABLE} ${GENERATE_SCRIPT} --man ${EVENT_MANIFEST} --exc ${EVENT_EXCLUSIONS} --intermediate ${CMAKE_CURRENT_BINARY_DIR} ${NONEXTERN_ARG})

execute_process(
  COMMAND ${GENERATE_COMMAND} --dry-run
  RESULT_VARIABLE GEN_EVENTPIPE_RESULT
  OUTPUT_VARIABLE GEN_EVENTPIPE_SOURCE_PATHS
  ERROR_VARIABLE GEN_EVENTPIPE_ERRORS
)

if (NOT GEN_EVENTPIPE_RESULT EQUAL 0)
  message(FATAL_ERROR "Failed to generate EventPipe: ${GEN_EVENTPIPE_ERRORS}")
endif()

string(REPLACE "\n" ";" GEN_EVENTPIPE_SOURCE_PATHS ${GEN_EVENTPIPE_SOURCE_PATHS}) # turn the outputted list of files into a CMake list

set (GEN_EVENTPIPE_SOURCES "")
foreach(GEN_EVENTPIPE_SOURCE_PATH ${GEN_EVENTPIPE_SOURCE_PATHS})
  file(TO_CMAKE_PATH ${GEN_EVENTPIPE_SOURCE_PATH} GEN_EVENTPIPE_SOURCE)
  list(APPEND GEN_EVENTPIPE_SOURCES ${GEN_EVENTPIPE_SOURCE})
endforeach()

add_custom_command(OUTPUT ${GEN_EVENTPIPE_SOURCES}
  COMMAND ${GENERATE_COMMAND}
  DEPENDS ${GENERATE_SCRIPT} ${EVENT_MANIFEST} ${EVENT_EXCLUSIONS})

set (CONTAINER_SOURCES "")
set (CONTAINER_HEADERS "")
set (EVENTPIPE_SOURCES "")
set (EVENTPIPE_HEADERS "")
set (CORECLR_EVENTPIPE_SHIM_SOURCES "")
set (CORECLR_EVENTPIPE_SHIM_HEADERS "")

set (SHARED_CONTAINERS_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/containers")
set (SHARED_EVENTPIPE_SOURCE_PATH "${CLR_SRC_NATIVE_DIR}/eventpipe")
set (CORECLR_EVENTPIPE_SHIM_SOURCE_PATH "${CORECLR_EVENTPIPE_SHIM_DIR}")

include (${SHARED_CONTAINERS_SOURCE_PATH}/containers.cmake)
include (${SHARED_EVENTPIPE_SOURCE_PATH}/eventpipe.cmake)

list(APPEND EVENTPIPE_SOURCES
  ${SHARED_EVENTPIPE_SOURCES}
  ${SHARED_DIAGNOSTIC_SERVER_SOURCES}
)

list(APPEND EVENTPIPE_HEADERS
  ${SHARED_EVENTPIPE_HEADERS}
  ${SHARED_DIAGNOSTIC_SERVER_HEADERS}
)

addprefix(CONTAINER_SOURCES ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_SOURCES}")
addprefix(CONTAINER_HEADERS ${SHARED_CONTAINERS_SOURCE_PATH} "${SHARED_CONTAINER_HEADERS}")

addprefix(EVENTPIPE_SOURCES ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_SOURCES}")
addprefix(EVENTPIPE_HEADERS ${SHARED_EVENTPIPE_SOURCE_PATH} "${EVENTPIPE_HEADERS}")

set_source_files_properties(${SHARED_EVENTPIPE_SOURCE_PATH}/ep-sources.c PROPERTIES COMPILE_DEFINITIONS EP_FORCE_INCLUDE_SOURCE_FILES)
set_source_files_properties(${SHARED_EVENTPIPE_SOURCE_PATH}/ds-sources.c PROPERTIES COMPILE_DEFINITIONS DS_FORCE_INCLUDE_SOURCE_FILES)

set_source_files_properties(${CONTAINER_SOURCES} PROPERTIES LANGUAGE CXX)
set_source_files_properties(${EVENTPIPE_SOURCES} PROPERTIES LANGUAGE CXX)

if(CLR_CMAKE_HOST_UNIX)
    set_source_files_properties(${CONTAINER_SOURCES} PROPERTIES COMPILE_OPTIONS -xc++)
    set_source_files_properties(${EVENTPIPE_SOURCES} PROPERTIES COMPILE_OPTIONS -xc++)
endif(CLR_CMAKE_HOST_UNIX)

list(APPEND CORECLR_EVENTPIPE_SHIM_SOURCES
  ep-rt-coreclr.cpp
)

list(APPEND CORECLR_EVENTPIPE_SHIM_HEADERS
  ds-rt-coreclr.h
  ds-rt-types-coreclr.h
  ep-rt-coreclr.h
  ep-rt-config-coreclr.h
  ep-rt-types-coreclr.h
)

addprefix(CORECLR_EVENTPIPE_SHIM_SOURCES ${CORECLR_EVENTPIPE_SHIM_SOURCE_PATH} "${CORECLR_EVENTPIPE_SHIM_SOURCES}")
addprefix(CORECLR_EVENTPIPE_SHIM_HEADERS ${CORECLR_EVENTPIPE_SHIM_SOURCE_PATH} "${CORECLR_EVENTPIPE_SHIM_HEADERS}")


list(APPEND EVENTPIPE_SOURCES
  ${CORECLR_EVENTPIPE_SHIM_SOURCES}
  ${CORECLR_EVENTPIPE_SHIM_HEADERS}
  ${EVENTPIPE_HEADERS}
  ${SHARED_EVENTPIPE_CONFIG_HEADERS}
  ${CONTAINER_SOURCES}
  ${CONTAINER_HEADERS}
)

add_library_clr(eventpipe_gen_objs OBJECT ${GEN_EVENTPIPE_SOURCES})
target_precompile_headers(eventpipe_gen_objs PRIVATE [["common.h"]])
set_target_properties(eventpipe_gen_objs PROPERTIES LINKER_LANGUAGE CXX)
add_dependencies(eventpipe_gen_objs eventing_headers)

add_library_clr(eventpipe_objs OBJECT ${EVENTPIPE_SOURCES})
target_precompile_headers(eventpipe_objs PRIVATE [["common.h"]])
set_target_properties(eventpipe_objs PROPERTIES LINKER_LANGUAGE CXX)
add_dependencies(eventpipe_objs eventing_headers)

add_library(eventpipe INTERFACE)
target_sources(eventpipe INTERFACE $<TARGET_OBJECTS:eventpipe_gen_objs> $<TARGET_OBJECTS:eventpipe_objs>)
